package com.cpkf.util;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JSonUtil {

	public static String transformToJSon(Object object) {
		ObjectMapper mapper = new ObjectMapper();
		StringWriter writer = new StringWriter();
		try {
			mapper.writeValue(writer, object);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return writer.toString();
	}

	public static <T> Object transformToObject(String data, Class<T> valueType) {
		ObjectMapper mapper = new ObjectMapper();
		Object object = null;
		try {
			object = mapper.readValue(data, valueType);
		} catch (JsonParseException e) {
			e.printStackTrace();
			return null;
		} catch (JsonMappingException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return object;
	}
	
	public static Map<String,String> transformJsonStringToMap(String jsonString) {
		Map<String,String> map = new HashMap<String,String>();
		if (StringUtils.isBlank(jsonString) || jsonString.length() <= 2) {
			return map;
		}
		
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
		mapper.configure(Feature.ALLOW_SINGLE_QUOTES,true);
		try {
//			map = mapper.readValue(jsonString, new TypeReference<HashMap<String,String>>(){});
			map = mapper.readValue(jsonString, HashMap.class);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	public static void main(String[] args) {
		String test = "{\"m_PhoneNumbers_phone-117224\":\"269-685-1977 \",\"m_PhoneNumbers_phoneExtension-117224\":\"\",\"m_Contacts_email-105547\":\"amber.rupe@thedistributionsolutions.com\",\"m_PhoneNumbers_fax-117225\":\"269-685-6789 \",\"m_PhoneNumbers_faxExtension-117225\":\"\",\"m_PhoneNumbers_phone-117362\":\"(269) 685-1977\",\"m_PhoneNumbers_phoneExtension-117362\":\"\",\"m_PhoneNumbers_fax-\":\"\",\"m_PhoneNumbers_faxExtension-\":\"\",\"m_PhoneNumbers_phone-117548\":\"269-685-1977\",\"m_PhoneNumbers_phoneExtension-117548\":\"\",\"Catalogs_addressLine1-105547\":\"901 Lincoln Parkway\",\"Catalogs_addressLine2-105547\":\"\",\"Catalogs_city-105547\":\"Plainwell\",\"Catalogs_state-105547\":\"MI\",\"m_EnglishStrings_storeConditions-2180621\":\"To Return a Product:<BR>\nCreate a SHOP RMA#.\nInstruct the customer to return their order to the following address:<BR>\nSHOP.COM<BR>\nReturns - RMA#<BR>\n901 Lincoln Parkway<BR>\nPlainwell, MI 49080<BR>\n<BR>\nIMPORTANT: Instruct the customer to include the packing slip in the box, otherwise, they will not get credit.<BR>\n<BR>\nDamaged Product: If the product was damaged in transit - SHOP needs to file a claim for the product and shipping with UPS directly and instruct the customer to reorder the product.<BR>\",\"vendorId\":\"105547\",\"updateButton\":\"Update\"}";
	}
}
